
{extend name="layout"}
{block name="title"}上传身份证{/block}
{block name="body"}

<div class="upload-content">
		<div class="upload-box">

			<div class="img-box">

				<div >
					<a  onclick="initUpload(this)">
						{if $increase.id_front eq true }
						<img src="{$increase.id_front|default=''}"  style=" width: 3.653rem;border: 1px solid #e6e6e6;" />
						{else/}
						<img src="__ROOT__/static/personal/images/idfront.png"  style=" width: 3.653rem;border: 1px solid #e6e6e6;" />
						{/if}
					</a>

					<input type="file" style="display: none" onchange="uploadImg(this)"
						   value="" >
					<input type="hidden" name="idfront" value="{$increase.id_front|default=''}" >

					<p><span>点击上传</span> <span>人像面</span></p>



				</div>
				<div>
					<a  onclick="$(this).next('input').click()">
						{if $increase.id_back eq true }
						<img src="{$increase.id_back|default=''}"  style=" width: 3.653rem;border: 1px solid #e6e6e6;" />
						{else/}
						<img src="__ROOT__/static/personal/images/idback.png"  style=" width: 3.653rem;border: 1px solid #e6e6e6;" />
						{/if}
					</a>

					<input type="file" style="display: none" onchange="uploadImg(this)"
						   value="" >
					<input type="hidden" name="idback" value="{$increase.id_back|default=''}">

					<p ><span>点击上传</span> <span>国徽面</span></p>
				</div>

			</div>
			<input type="hidden" name="id" value="{$id|default=0}">
			<button  onclick="submitUpload()">确 定</button>

		</div>



</div>

{/block}
{block name="script"}
<script>
	$(document).ready(function(){


	})
	isOk = true
	var initUpload = function (that){
		if(isOk) {

			this.isOk = false
			setTimeout(function () {
				isOk = true
			}, 2000)
			$(that).next('input').click()
		}else{
			layer.msg('请稍等~');
		}
	}

	var uploadImg = function (obj){


			var $this = $(obj)

			var filePath = $this[0].files[0].name      //获取到input的value，里面是文件的路径
			var fileFormat = filePath.split('.')[1].toLowerCase()
			// 检查是否是图片
			if( !fileFormat.match(/png|jpg|jpeg/) ) {
				layer.msg('上传错误,文件格式必须为：png/jpg/jpeg');

				return
			}
			var formData =  new FormData();
			formData.append('image', $this[0].files[0]); //添加图片信息的参数
			$.ajax({
				url: '{:url("upload")}',
				type: "post",
				dataType: "json",
				cache: false,
				mimeType: "multipart/form-data",
				data: formData,
				processData: false,// 不处理数据
				contentType: false, // 不设置内容类型
				async:false,
				success:function (res){
					if(res.code==1){
						layer.msg(res.info);
						$this.next('input').val(res.data)
						$this.prev('a').children('img').attr('src', res.data)

					}else{
						layer.msg(res.info);
					}

				},
				fail:function (){
					layer.msg("上传失败！");
				}
			});



	}
	isSubmit = true
	var submitUpload = function (){
		//
		if(isSubmit){
			isSubmit = false
			setTimeout(function (){
				isSubmit = true
			},2000)

			var idfront = $("[name='idfront']").val()
			var idback = $("[name='idback']").val()
			console.log(idfront)
			console.log(idback)
			if(!idfront){
				layer.msg("请上传身份证正面~！");
				return
			}
			if(!idback){
				layer.msg("请上传身份证背面~！");
				return
			}
			$.ajax({
				url: '{:url("idupload")}',
				data: {
					"idfront":idfront,
					"idback":idback,
					"id":$("[name='id']").val(),

				},
				type: "post",
				success: function(res) {
					if(res.code == 1) {
						layer.msg(res.info)
						setTimeout(function (){
							window.location.href="index.html"
						},1000)

					} else {
						layer.msg(res.info)
					}
				},
				error: function() {
					layer.msg("发生错误~");
				}
			});
		}

	}




</script>
{/block}
{block name="style"}
<style>

</style>
{/block}
